package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.saas.domain.dao.CreditCardBillDao;
import com.xbongbong.saas.domain.entity.ext.CreditCardBillEntityExt;
import com.xbongbong.saas.model.CreditCardBillModel;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author xingxing.xiao
 */
@Service("creditCardBillModel")
public class CreditCardBillModelImpl implements CreditCardBillModel {

    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private CreditCardBillDao creditCardBillDao;

    @Override
    public Integer insert(CreditCardBillEntityExt entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        Integer success = creditCardBillDao.insert(entity);
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        BeanUtil.copyProperties(entity, paasFormDataEntity);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_CREDIT_CARD_BILL);
        paasEsModel.insert(insertDTO);
        return success;
    }

    @Override
    public void insertBatch(List<CreditCardBillEntityExt> list, boolean isImport) throws XbbException {
        creditCardBillDao.insertBatch(list);
        //ES数据插入
        List<InsertDTO> insertList = new ArrayList<>();
        for (CreditCardBillEntityExt item : list) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(item.getCorpid() + "_" + item.getId());
            insertDTO.setSource(BeanUtil.convertBean2Map(item, true, true));
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CREDIT_CARD_BILL);
            insertList.add(insertDTO);
        }
        if (isImport) {
            paasEsModel.insertBatch(insertList, WriteRequest.RefreshPolicy.NONE);
        } else {
            paasEsModel.insertBatch(insertList);
        }
    }

    @Override
    public List<CreditCardBillEntityExt> findEntities(Map<String, Object> param) {
        return creditCardBillDao.findEntities(param);
    }

    @Override
    public Integer getEntityCount(Map<String, Object> param) {
        return creditCardBillDao.getEntitiesCount(param);
    }

    @Override
    public Integer update(CreditCardBillEntityExt entity) throws XbbException {
        Integer success = creditCardBillDao.update(entity);
        if (Objects.equals(1, success)) {
            PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
            BeanUtil.copyProperties(entity, paasFormDataEntity);
            InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_CREDIT_CARD_BILL);
            paasEsModel.update(insertDTO);
        }
        return success;
    }

    @Override
    public CreditCardBillEntityExt getByKey(Long key, String corpid) {
        return creditCardBillDao.getByKey(key, corpid);
    }

    @Override
    public Integer updateBatch(List<UpdateDataEntity> list, String corpid) throws XbbException {
        Integer integer = 0;
        if (CollectionsUtil.isNotEmpty(list)) {
            integer = creditCardBillDao.updateBatch(list, corpid);
            paasEsModel.updateBatchMuchField(list, IndexTypeEnum.IDX_SAAS_CREDIT_CARD_BILL);
        }
        return integer;
    }

}
